using System;
using System.Collections;
using System.Collections.Generic;
using MyFrame.Single;
using Platform;
using Platform.Skill.Core;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
using static Entity;
using static UnityEngine.InputSystem.InputAction;

/// <summary>
/// 输入控制器
/// </summary>
public partial class Player
{
    private Vector2 MoveVector => moveAction.ReadValue<Vector2>();
    private bool HasJumpInput => jumpAction.ReadValue<float>() > 0;

    private void InputBinding()
    {
        InitActions();
        // Subscribe to action events
        aimSwordAction.performed += OnAimSword;
        dashAction.performed += OnDash;
        jumpAction.performed += OnJump;
        attackAction.performed += OnAttack;
        heavyAttackAction.performed += OnHeavyAttack;
        counterAttackAction.performed += OnCounterAttack;

        // inputActions.Player.AimSword.performed += OnAimSword;
        // inputActions.Player.Dash.performed += OnDash;
        // inputActions.Player.Jump.performed += OnJump;
        // inputActions.Player.Attack.performed += OnAttack;
        // // 反击和重击绑定同一个按键，根据动画的有无来决定执行
        // inputActions.Player.HeavyAttack.performed += OnHeavyAttack;
        // inputActions.Player.CounterAttack.performed += OnCounterAttack;

        // inputActions.Player.UseSkill1.performed += OnUseSkill1;
        // inputActions.Player.UseSkill2.performed += OnUseSkill2;
        // inputActions.Player.UseSkill3.performed += OnUseSkill3;
        // inputActions.Player.UseSkill4.performed += OnUseSkill4;
        // inputActions.Player.UseSkill5.performed += OnUseSkill5;
    }


    private void OnUseSkill2(CallbackContext context)
    {
        // TryUseSkill(SkillEnum.ExplodeCrstal);
        //todo: 临时
        this.anim.GetComponent<Animator>().runtimeAnimatorController = AAMgr.LoadAsset<RuntimeAnimatorController>("WomanWarrior2");
    }

    private void OnUseSkill3(CallbackContext context)
    {
        TryUseSkill(SkillEnum.Crystal);
    }

    private void OnUseSkill4(CallbackContext context)
    {
        // TryUseSkill(SkillEnum.Blackhole);
        ChangeState(States.Blackhole);
    }

    private void OnUseSkill5(CallbackContext context)
    {
        var other = Tool.FindClosestEnemy(transform);
        if (other == null)
            return;
        // 获取对方的 Animator 组件
        Animator otherAnimator = other.GetComponentInChildren<Animator>();
        if (otherAnimator != null)
        {
            // 获取自己的 Animator 组件
            Animator playerAnimator = GetComponentInChildren<Animator>();
            // playerAnimator.transform.position += Vector3.up * 0.6f;
            if (playerAnimator != null)
            {
                // 将对方的动画控制器设置到自己身上
                playerAnimator.runtimeAnimatorController = otherAnimator.runtimeAnimatorController;
            }
        }

        // 获取对方的碰撞盒组件
        CapsuleCollider2D otherCollider = other.GetComponent<CapsuleCollider2D>();
        if (otherCollider != null)
        {
            // 获取对方的大小和偏移
            Vector3 otherSize = otherCollider.size;
            Vector3 otherCenter = otherCollider.offset;

            // 获取自己身上的碰撞盒组件
            CapsuleCollider2D selfCollider = GetComponent<CapsuleCollider2D>();
            if (selfCollider != null)
            {
                // 设置自己的大小和偏移
                selfCollider.size = otherSize;
                selfCollider.offset = otherCenter;
                selfCollider.offset = new Vector2(selfCollider.offset.x, 1.8f);
            }
        }
    }

    private void OnUseSkill1(CallbackContext context)
    {
        TryUseSkill(currentSkill);
        // switch (currentSkill)
        // {
        //     case SkillEnum.ReBoundSword:
        //         currentSkill = SkillEnum.PierceSword;
        //         break;
        //     case SkillEnum.PierceSword:
        //         currentSkill = SkillEnum.SpinSword;
        //         break;
        //     case SkillEnum.SpinSword:
        //         currentSkill = SkillEnum.flySword;
        //         break;
        //     case SkillEnum.flySword:
        //         currentSkill = SkillEnum.ReBoundSword;
        //         break;
        // }
    }

    private void OnAimSword(CallbackContext context)
    {
        if (HasSword())
            return;
        ChangeState(States.AimSword);

        // 打开技能指示器
        OpenSkillIndicator(currentSkill);
    }

    private void OnCounterAttack(CallbackContext context)
    {
        if (HasStateAnim(States.CounterAttack))
            ChangeState(States.CounterAttack);
    }

    // 轻攻击按住连续执行就不在这里判断了
    private void OnAttack(CallbackContext context)
    {
        LightAttack();
    }

    private void OnHeavyAttack(CallbackContext context)
    {
        if (HasStateAnim(States.HeavyDoubleAttack.ToString() + 1))
            HeavyAttack();
    }

    private void OnJump(CallbackContext ctx)
    {
        Jump();
    }

    private void OnDash(CallbackContext ctx)
    {
        Dash();
    }
}
